package com.asset.dispoal.poi.documentation.service.commom;

import com.asset.dispoal.poi.documentation.domain.db.ConfItemDo;
import com.asset.dispoal.poi.documentation.domain.db.mapper.DocumentationMapper;
import com.asset.dispoal.poi.documentation.dto.DataTableDocumentationRequest;
import com.asset.dispoal.poi.documentation.util.DefineMethodPolicy;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.file.FileUtil;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.io.File;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class DownloadDataTableCommand implements BaseCommand<DataTableDocumentationRequest, Result> {

    @Autowired
    private DocumentationMapper documentationMapper;

    @Autowired
    private Environment environment;

    @Autowired
    BaseIdeable<String> ideable;

    @SneakyThrows
    @Override
    public Result execute(DataTableDocumentationRequest request) {
        List<ConfItemDo> doList = documentationMapper.findConfItemDetailById(request.getEvidenceConfId(), request.getAppointId());
        String specialRemark = documentationMapper.findSpecialRemarkById(request.getAppointId());
        if (StringUtil.isEmptyNull(specialRemark)) {
            specialRemark = "";
        }
        AtomicInteger index = new AtomicInteger(0);
        for (ConfItemDo itemDo : doList) {
            itemDo.setIndex(index.addAndGet(1));
        }
        Map<String, Object> listMap = new HashMap<>();
        listMap.put("list", doList);
        listMap.put("specialRemark", specialRemark);
//        listMap.put("image","d:\\成都债清.png");
//        listMap.put("image", Pictures.ofLocal("d:\\成都债清.png").size(200,200).create());
        Configure config = Configure.builder()
//                .addPlugin('%',new SealPictureRenderPolicy())
                .bind("list", new DefineMethodPolicy())
                .build();
        XWPFTemplate template = XWPFTemplate.compile(environment.getProperty("file.evidence.url") + environment.getProperty("file.dataTable.url") + File.separator + environment.getProperty("file.dataTable.template"), config).render(listMap);
        String headerUrl = environment.getProperty("file.evidence.url");
        String bodyUrl = environment.getProperty("file.dataTable.url") + File.separator + LocalDate.now() + File.separator + ideable.generateId() + ".docx";
        File checkDir = new File(headerUrl + bodyUrl);
        if (!checkDir.getParentFile().exists()) {
            FileUtil.createDirectory(checkDir.getParentFile(), true);
        }
        template.writeToFile(headerUrl + bodyUrl);
        template.close();
        return Result.ok(bodyUrl);
    }
}
